home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / programm / GNUC / UTIL-41S.LZH / util-41 / printstk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-05  |  5.0 KB  |  245 lines

  1. /* 
  2.  * utility to print the value of _stksize from gcc-cc1.ttp
  3.  *
  4.  *    Usage: printstk [<filename>]
  5.  *        if <filename> is not specified defaults to .\gcc-cc1.ttp
  6.  *    ++jrb
  7.  *
  8.  *      modified to print a value of _initial_stack in cases when this
  9.  *      is defined instead of _stksize -- mj
  10.  */
  11.  
  12. #include <stdio.h>
  13.  
  14. #ifdef CROSSATARI
  15. #include "cross-inc/st-out.h"
  16. #else
  17. #include <st-out.h>
  18. #endif
  19.  
  20. #if __STDC__
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #else
  25. #include <string.h>
  26. extern char *malloc();
  27. extern long lseek();
  28. #define size_t unsigned long
  29. #endif
  30.  
  31. #ifndef FILENAME_MAX
  32. # define FILENAME_MAX 128
  33. #endif
  34.  
  35. #ifdef WORD_ALIGNED
  36. # define SIZEOF_AEXEC ((2*sizeof(short)) + (6*sizeof(long)))
  37. # ifndef SYMLEN
  38. #   define SYMLEN 8
  39. # endif
  40. # define SIZEOF_ASYM  ((SYMLEN*sizeof(char)) + sizeof(short) + sizeof(long))
  41. # define SYM_OFFSET   (sizeof(short) + (3*sizeof(long)))
  42. #endif
  43. #ifdef BYTE_SWAP
  44.  
  45. #define SWAP4(y) (((unsigned)(y)>>24) + (((unsigned)(y)>>8)&0xff00) + \
  46.          (((unsigned)(y)<<8)&0xff0000) + ((unsigned)(y)<<24)) 
  47. #define SWAP2(y) ((((unsigned)(y)&0xff00)>>8) + (((unsigned)(y)&0x00ff)<<8))
  48.  
  49. #endif /* BYTE_SWAP */
  50.  
  51. extern void dump_version(char *prog);
  52. char *progname = "printstk";
  53.  
  54. int error = 0;
  55.  
  56. static char *sym_names[] = { "__stksize", "__initial_stack" };
  57.  
  58. long find_offset (fd, fn, what)
  59. int fd;
  60. char *fn;
  61. int *what;
  62. {
  63.     struct aexec head;
  64.     struct asym  sym;
  65.     int    all = 0;
  66.     int index = 1;
  67. #ifndef WORD_ALIGNED
  68.     char extension[sizeof (struct asym)];
  69. #else
  70.     char extension[SIZEOF_ASYM];
  71. #endif
  72.     
  73. #ifndef WORD_ALIGNED
  74.     if(read(fd, &head, sizeof(head)) != sizeof(head))
  75. #else
  76.     if(read_head(fd, &head))
  77. #endif
  78.     {
  79.     perror(fn);
  80.     return -1;
  81.     }
  82. #ifdef BYTE_SWAP
  83.     head.a_magic = SWAP2(head.a_magic);
  84.     head.a_syms = SWAP4(head.a_syms);
  85.     head.a_text = SWAP4(head.a_text);
  86.     head.a_data = SWAP4(head.a_data);
  87. #endif
  88.     if(head.a_magic != CMAGIC)
  89.     {
  90.     fprintf(stderr,"%s: invalid magic number %x\n", fn, head.a_magic);
  91.     return -1;
  92.     }
  93.     if(head.a_syms == 0)
  94.     {
  95.     fprintf(stderr,"%s: no symbol table\n", fn);
  96.     return -1;
  97.     }
  98.     if(lseek(fd, head.a_text+head.a_data, 1) != 
  99. #ifndef WORD_ALIGNED
  100.        (head.a_text+head.a_data+sizeof(head)))
  101. #else
  102.        (head.a_text+head.a_data+SIZEOF_AEXEC))
  103. #endif
  104.     {
  105.     perror(fn);
  106.     return -1;
  107.     }
  108.     for(;;)
  109.     {
  110. #ifndef WORD_ALIGNED
  111.     if(index && (read(fd, &sym, sizeof(sym)) != sizeof(sym)))
  112. #else
  113.     if(index && read_sym(fd, &sym))
  114. #endif
  115.     {
  116.         fprintf(stderr, "%s: symbol _stksize not found\n", fn);
  117.         return -1;
  118.     }
  119. #ifdef BYTE_SWAP
  120.     sym.a_type = SWAP2(sym.a_type);
  121.     sym.a_value = SWAP4(sym.a_value);
  122. #endif
  123.     if (index && (sym.a_type & A_LNAM) == A_LNAM)
  124.       if (read (fd, extension, sizeof (extension)) != sizeof (extension))
  125.         {
  126.           fprintf (stderr, "%s: symbol _stksize not found\n", fn);
  127.           return -1;
  128.         }
  129.     /* after symbol read check first for _stksize */
  130.     index ^= 1;
  131.     if (strncmp(sym_names[index], sym.a_name, 8) == 0)
  132.     {
  133.       if ((sym.a_type & A_LNAM) == A_LNAM
  134.           && strncmp (sym_names[index] + 8, extension, sizeof (extension)))
  135.         continue;
  136.       if (sym.a_type & A_DATA)
  137.         break;
  138.       if (all++)
  139.         {
  140.           fprintf (stderr, "%s: symbol _stksize is undefined\n", fn);
  141.           return -1;
  142.         }
  143.     }
  144.     }
  145.     
  146.     *what = index;
  147. #ifndef WORD_ALIGNED
  148.     return sym.a_value + sizeof(head);
  149. #else
  150.     return sym.a_value + SIZEOF_AEXEC;
  151. #endif
  152. }
  153.  
  154. int main(argc, argv)
  155. int argc;
  156. char **argv;
  157. {
  158.   if (argv[0][0] != '\0')
  159.     progname = argv[0];
  160.  
  161.   if (argc == 2 && strcmp(argv[1], "-v") == 0)
  162.   {
  163.     dump_version(progname);
  164.     exit(0);
  165.   }
  166.   while (--argc)
  167.     error |= print_stack (*++argv);
  168.   exit (error);
  169. }
  170.  
  171. int
  172. print_stack (fn)
  173.      char *fn;
  174. {
  175.     int fd;
  176.     int what;
  177.     long stksize, offset;
  178.     
  179.     if((fd = open(fn, 0)) < 0)
  180.     {
  181.     perror(fn);
  182.     return 1;
  183.     }
  184.     
  185.     offset = find_offset(fd, fn, &what);
  186.     if (offset < 0)
  187.       {
  188.     close (fd);
  189.     return 1;
  190.       }
  191.  
  192.     if(lseek(fd, offset, 0) != offset)
  193.     {
  194.     perror(fn);
  195.     close (fd);
  196.     return 1;
  197.     }
  198.     read(fd, &stksize, sizeof(long));
  199. #ifdef BYTE_SWAP
  200.     stksize = SWAP4(stksize);
  201. #endif
  202.     printf("%s: %s is %ld (%dK)\n",
  203.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  204.     
  205.     return close(fd) != 0;
  206. }
  207.  
  208. #ifdef WORD_ALIGNED
  209. #ifndef atarist
  210. # define lread  read
  211. # define lwrite write
  212. #endif
  213.  
  214. /*
  215.  * read header -- return !0 on err
  216.   */
  217. #define ck_read(fd, addr, siz) \
  218.   if((long)siz != lread(fd, addr, (long)siz)) return !0;
  219.   
  220. int read_head (fd, a)
  221. int fd;
  222. struct aexec *a;
  223. {
  224.     ck_read(fd, &a->a_magic,   sizeof(a->a_magic));
  225.     ck_read(fd, &a->a_text,    sizeof(a->a_text));
  226.     ck_read(fd, &a->a_data,    sizeof(a->a_data));
  227.     ck_read(fd, &a->a_bss,     sizeof(a->a_bss));
  228.     ck_read(fd, &a->a_syms,    sizeof(a->a_syms));
  229.     ck_read(fd, &a->a_AZero1,  sizeof(a->a_AZero1));
  230.     ck_read(fd, &a->a_AZero2,  sizeof(a->a_AZero2));
  231.     ck_read(fd, &a->a_isreloc, sizeof(a->a_isreloc));
  232.     return 0;
  233. }
  234.  
  235. int read_sym(fd, s)
  236. int fd;
  237. struct asym *s;
  238. {
  239.     ck_read(fd, s->a_name, 8);
  240.     ck_read(fd, &(s->a_type), sizeof(short));
  241.     ck_read(fd, &(s->a_value), sizeof(long));
  242.     return 0;
  243. }
  244. #endif
  245.